<template>
  <div id="userLoginPage">
    <h2 class="title">杉果云图库-用户登录</h2>
    <h2 class="desc">企业级智能协同云图库</h2>
    <a-form :model="formState" name="basic" autocomplete="off" @finish="doSubmit">
      <a-form-item name="userAccount" :rules="[{ required: true, message: '请输入账号!' }]">
        <a-input
          v-model:value="formState.userAccount"
          aria-placeholder="请输入账号"
          placeholder="请输入账号"
        />
      </a-form-item>

      <a-form-item
        name="userPassword"
        :rules="[
          { required: true, message: '请输入密码!' },
          { min: 8, message: '密码长度不能小于8位' },
        ]"
      >
        <a-input-password
          v-model:value="formState.userPassword"
          aria-placeholder="请输入密码"
          placeholder="请输入密码"
        />
      </a-form-item>
      <a-form-item>
        <div class="tips">
          没有账号？
          <router-link to="/user/register"> 点击注册 </router-link>
        </div>
      </a-form-item>

      <a-form-item>
        <a-button type="primary" html-type="submit" style="width: 100%">登录</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<script setup lang="ts">
import { reactive } from 'vue'
import { userLoginUsingPost } from '@/api/yonghu'
import { message } from 'ant-design-vue'
import { useLoginUserStore } from '../../stores/useLoginUserStore'
import router from '@/router'
const loginUserStore = useLoginUserStore()
const formState = reactive<API.UserLoginRequest>({
  userAccount: '',
  userPassword: '',
})
const doSubmit = (values: any) => {
  handerSubmit(values)
}

const handerSubmit = async (values: any) => {
  const res = await userLoginUsingPost(values)
  if (res.data.code === 0 && res.data.data) {
    loginUserStore.fetchLoginUser()
    message.success('登录成功')
    router.push('/')
  } else {
    message.error(res.data.message)
  }
}
</script>

<style scoped>
#userLoginPage {
  width: 360px;
  margin: 0 auto;
}
.title {
  margin-top: 32px;
  text-align: center;
  margin-bottom: 16px;
  font-size: 32px;
}
.desc {
  color: #bbb;
  text-align: center;
  margin-bottom: 32px;
}
.tips {
  font-size: 13px;
  margin-bottom: 16px;
  text-align: right;
  color: #bbb;
}
</style>
